package com.contract.commonlib.permission

import android.app.Activity
import androidx.fragment.app.FragmentActivity
import com.contract.commonlib.base.BaseActivity
import com.contract.commonlib.base.BaseViewModel
import com.permissionx.guolindev.PermissionX

/**
 * @author zhengjun
 * @desc
 * @create at 2020/5/13 14:55
 */
object PermissionManager {

    fun checkPermission(
        context: FragmentActivity?,
        permissiionSuccess: onPermissiionSuccessListener,
        vararg permissions: String?
    ) {
        PermissionX.init(context)
            .permissions(*permissions)
            .onExplainRequestReason { scope, deniedList, beforeRequest ->
                scope.showRequestReasonDialog(
                    deniedList,
                    "即将申请的权限是程序必须依赖的权限",
                    "我已明白"
                )
            }
            .onForwardToSettings { scope, deniedList ->
                scope.showForwardToSettingsDialog(
                    deniedList,
                    "您需要去应用程序设置当中手动开启权限",
                    "我已明白"
                )
            }
            .request { allGranted, grantedList, deniedList ->
                if (allGranted) {
                    permissiionSuccess.onPermissionSuccess(1, grantedList)
                } else {
                    permissiionSuccess.onPermissionFail(deniedList)
                }
            }
    }

    fun checkPermission(
        context: FragmentActivity?,
        permissiionSuccess: onPermissiionSuccessListener,
        permissions: List<String?>?
    ) {
        PermissionX.init(context)
            .permissions(permissions)
            .onExplainRequestReason { scope, deniedList, beforeRequest ->
                scope.showRequestReasonDialog(
                    deniedList,
                    "即将申请的权限是程序必须依赖的权限",
                    "我已明白"
                )
            }
            .onForwardToSettings { scope, deniedList ->
                scope.showForwardToSettingsDialog(
                    deniedList,
                    "您需要去应用程序设置当中手动开启权限",
                    "我已明白"
                )
            }
            .request { allGranted: Boolean, grantedList: List<String>, deniedList: List<String>? ->
                if (allGranted) {
                    permissiionSuccess.onPermissionSuccess(1, grantedList)
                } else {
                    permissiionSuccess.onPermissionFail(deniedList)
                }
            }
    }

    interface onPermissiionSuccessListener {
        fun onPermissionSuccess(requestCode: Int, grantPermissions: List<String>)
        fun onPermissionFail(deniedList: List<String>?)
    }
}